package com.lwhtarena.netty.netty4.util;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ByteArrayToFileExample {

    public static void wireByteArray(byte[] res,String path){
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);

            FileChannel fileChannel = fileOutputStream.getChannel();

            // 将字节数组包装为 ByteBuffer
            ByteBuffer buffer = ByteBuffer.wrap(res);

            // 写入 ByteBuffer 到 FileChannel
            fileChannel.write(buffer);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String filePath = "path/to/your/file.txt";
        byte[] byteArray = "Hello, ByteArray to File!".getBytes();

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);

            FileChannel fileChannel = fileOutputStream.getChannel();

            // 将字节数组包装为 ByteBuffer
            ByteBuffer buffer = ByteBuffer.wrap(byteArray);

            // 写入 ByteBuffer 到 FileChannel
            fileChannel.write(buffer);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


